<?php

namespace app\member\controller;

/** 控制器类引入区 **/

/** 模型类引入区 **/
use app\common\model\VipOrder as VipOrderM;
use app\common\model\Comments as CommentsM;

/** 核心或第三方类引入区 **/

use my\Wx;
use think\Db;
use think\Validate;

/**
 *  订单控制器
 * @package app\api\controller
 */
class Order extends Base
{
    public $noNeedLogin = [

    ];

    /**
     *   创建 门店项目  订单   -> 购物车流程
     */
    public function createProject()
    {
        $user_id        = $this->user_id;
        $store_id       = $this->store_id;
        $ids            = $this->request->post('ids',"");
        $room_id        = $this->request->post('room_id',40);
        $type           = $this->request->post('type',"");
        if(empty($ids)){
            $this->result('项目id不能为空','',204);
        }

        $VipOrderM = new VipOrderM();
        $result = $VipOrderM->toPjAdd($user_id,$store_id, $ids,$room_id);
        if($result['status']){
            $this->result('下单成功',$result['data'],200);
        }else{
            $this->result($result['msg'],'',204);
        }
    }

    /**
     *   创建 门店商品  订单    -> 购物车流程
     */
    public function createProduct()
    {
        $user_id        = $this->user_id;
        $store_id       = $this->store_id;
        $room_id        = $this->request->post('room_id',40);
        $ids            = $this->request->post('ids',"");

        if(empty($ids)) $this->result('商品不存在','',204);

        $VipOrderM = new VipOrderM();
        $result = $VipOrderM->toPdAdd($user_id,$store_id, $ids,$room_id);
        if($result['status']){
            $this->result('下单成功',$result['data'],200);
        }else{
            $this->result($result['msg'],'',204);
        }
    }


    /**
     *  订单列表
    **/
    public function orderList()
    {
        $user_id   = $this->user_id;
        $store_id  = $this->store_id;
        $type      = $this->request->post('type',1);   //类型:   1=门店项目   2=门店商品
        $status    = $this->request->post('status',0);
        $limit     = $this->request->post('limit',10);

        $VipOrderM = new VipOrderM();
        $list = $VipOrderM->orderList($type,$user_id,$store_id,$status,$limit);

        $this->result('获取成功',$list,200);

    }

    /**
     *  订单详情
     **/
    public function orderInfo()
    {
        $user_id   = $this->user_id;
        $store_id  = $this->store_id;
        $type      = $this->request->post('type',1);   //类型:   1=门店项目   2=门店商品
        $id        = $this->request->post('id',0);

        if(empty($id)) $this->result('请选择订单','',204);

        $VipOrderM = new VipOrderM();
        $list = $VipOrderM->orderInfo($type,$user_id,$store_id,$id);

        $this->result('获取成功',$list,200);
    }



    /**
     *  订单退款
    **/
    public function orderRefund()
    {
        $user_id   = $this->user_id;
        $store_id  = $this->store_id;
        $type      = $this->request->post('type',1);   //类型:   1=门店项目   2=门店商品
        $id        = $this->request->post('id',0);

        if(empty($id)) $this->result('请选择订单','',204);

        $VipOrderM = new VipOrderM();
        $res = $VipOrderM->refund($type,$user_id,$store_id,$id);

        if($res){
            $this->result('退款成功','',200);
        }else{
            $this->result('退款失败','',204);
        }
    }

    /**
     * 取消订单
     **/
    public function orderCancel()
    {
        if($this->request->isPost()){
            $store_id   = $this->store_id;
            $type       = $this->request->post('type',0);
            $id         = $this->request->post('id',0);
            if(empty($id))  $this->result('请选择订单','',204);
            $VipOrderM = new VipOrderM();
            $result = $VipOrderM->cancel($type,$this->user_id,$store_id,$id);
            if($result){
                $this->result('取消成功','',200);
            }else{
                $this->result('取消失败','',204);
            }
        }else{
            $this->result('非法请求','',204);
        }
    }



    /**
     * 订单支付
     **/
    public function orderPay()
    {
        if($this->request->isPost()){
            $store_id   = $this->store_id;
            $type       = $this->request->post('type',0);
            $id         = $this->request->post('id',0);
            if(empty($id))  $this->result('请选择订单','',204);
            $VipOrderM = new VipOrderM();
            $result = $VipOrderM->pay($type,$this->user_id,$store_id,$id);
            if($result ){
                $this->result('支付成功','',200);
            }else{
                $this->result('支付失败','',204);
            }
        }else{
            $this->result('非法请求','',204);
        }
    }


    /**
     *  评论
    **/
    public function setComment()
    {
        $user_id        = $this->user_id;
        $order_id       = $this->request->post('order_id');
        $store_id       = $this->request->post('store_id');
        $engineer_id    = $this->request->post('engineer_id',0);
        $content        = $this->request->post('content','');
        $images         = $this->request->post('images');
        if(empty($content))  $this->result('请填写评论内容','',204);

        $CommentsM  = new CommentsM();
        $result     = $CommentsM->addComment($user_id,$store_id,$order_id,$engineer_id,$content,$images);
        if($result){
            $this->result('评论成功','',200);
        }else{
            $this->result('评论失败','',204);
        }

    }


    /**
     *  查询正在进行中的服务信息
    **/
    public function getCarryOrder()
    {
        $store_id  = $this->store_id;
        $room_id   = $this->request->post('room_id',40);
        $VipOrderM = new VipOrderM();
        $data      = $VipOrderM->carryOrder($store_id,$room_id);

        $this->result('获取成功',$data,200);
    }


    /**
     *  房间码 单点技师  ->  普通流程
    **/
    public function spotEngineer()
    {
        $user_id        = $this->user_id;
        $store_id       = $this->request->post('store_id',40);
        $room_id        = $this->request->post('room_id',40);
        $engineer_id    = $this->request->post('engineer_id',0);
        $project_id     = $this->request->post('project_id',0);

        if(empty($engineer_id)) $this->result('请选择服务技师','',204);
        if(empty($project_id)) $this->result('请选择服务项目','',204);

        $VipOrderM = new VipOrderM();

        $data = Db::name('qt_room')
            ->field('id,name,status_id')
            ->where(['id' => $room_id, 'store_id' => $store_id])
            ->whereNotIn('status_id',[2,3,10])->find();
        if (!empty($data)) {
            $room_status = Db::name('qt_room_status')->where('store_id',$store_id)->column('name','id');
            $this->result('房间'.$room_status[$data['status_id']].'不能下单', '', 204);
        }

        //判断技师是否空闲
        $user_data = Db::name('qt_order_queue')->where('user_id',$engineer_id)->where('status',1)->find();
        if (empty($user_data)) {
            $this->result('技师不是空闲中，不能下单','',200);
        }

        $result    = $VipOrderM->spotAdd($user_id,$store_id, $room_id,$engineer_id,$project_id);
        if ($result['status'] == true) {
            $this->result($result['msg'],'',200);
        }else {
            $this->result('下单失败'.$result['msg'],'',204);
        }
    }


    /**
     *  自助结账 页面信息
    **/
    public function getSelfOrder()
    {
        $user_id  = $this->user_id;
        $store_id = $this->store_id;
        $room_id  = $this->room_id;

        $VipOrderM = new VipOrderM();
        $result    = $VipOrderM->selfOrderInfo($user_id,$store_id, $room_id);
        if($result['status']){
            $this->result('获取成功',$result['data'],200);
        }else{
            $this->result($result['msg'],[],204);
        }

    }

    /**
     *  自助结账下单
    **/
    public function createSelf()
    {
        $user_id  = $this->user_id;
        $store_id = $this->store_id;
        $room_id  = $this->room_id;

        $order_id        = $this->request->post('order_id',0);
        $product_ids     = $this->request->post('product_ids',0);

        if(empty($order_id))            $this->result('请选择订单技师','',204);
        if(empty($product_ids))         $this->result('请选择服务项目','',204);

        $VipOrderM = new VipOrderM();
        $result    = $VipOrderM->createSelf($user_id,$store_id, $room_id,$order_id,$product_ids);
        if($result['status']){
            $this->result('下单成功',$result['data'],200);
        }else{
            $this->result($result['msg'],'',204);
        }
    }


}
